package cc.core.pullrefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import cc.core.pullrefresh.extra.BaseFooterLayout;

/**
 * Created by Administrator on 2016/2/1.
 * PullLoadMoreListView only support pull up to load more,
 * not support pull refresh.You can use this view with SwipeRefreshLayout.
 */
public class PullLoadMoreListView extends ListView implements AbsListView.OnScrollListener, IPullBase<ListView> {

    private OnScrollListener scrollListener;
    private OnRefreshListener refreshListener;
    protected int firstVisibleItem;
    protected int lastVisiablePosition;
    protected int mode;
    protected int totalCount;
    private int scrollState;// 当前滑动的状态
    private BaseFooterLayout footerView;
    // 当数据无法填充屏幕时无需加载更多
    private boolean canLoadMore = true;
    protected int state;

    public PullLoadMoreListView(Context context) {
        super(context);
    }

    public PullLoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullLoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (scrollListener != null) scrollListener.onScrollStateChanged(absListView, i);
        // 判断是否滑动了最底部
        if (lastVisiablePosition >= totalCount - 1 && scrollState == SCROLL_STATE_IDLE && canLoadMore) {
            if (refreshListener != null) {
                if (mode == PullRefreshBase.Mode.DISABLE || mode == PullRefreshBase.Mode.PULL_FROM_TOP)
                    return;
                if (state != PullRefreshBase.State.FOOTER_REFRESHING && state != PullRefreshBase.State.HEAD_REFRESHING) {// 已经处于刷新状态或者加载更多状态不需要再回调
                    state = PullRefreshBase.State.FOOTER_REFRESHING;
                    footerView.setVisibility(View.VISIBLE);
                    refreshListener.onFooterRefresh();
                    refreshListener.onFooterRefresh(footerView);
                }
            }

        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (scrollListener != null)
            onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        this.firstVisibleItem = firstVisibleItem;

        // 当数据不能填充整个屏幕时，将隐藏FooterView
        if (totalItemCount <= visibleItemCount) {
            this.canLoadMore = false;
            if (footerView != null) footerView.setVisibility(GONE);
        } else {
            this.canLoadMore = true;
        }

        lastVisiablePosition = firstVisibleItem + visibleItemCount;
        totalCount = totalItemCount;
        if (scrollListener != null) {
            scrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        }
    }

    public void setScrollListener(OnScrollListener scrollListener) {
        this.scrollListener = scrollListener;
    }

    @Override
    public ListView getRefreshView() {
        return null;
    }

    @Override
    public void startHeadAnim(float... value) {

    }

    @Override
    public boolean readyPull(MotionEvent ev) {
        return false;
    }

    @Override
    public void doMove(MotionEvent ev) {

    }

    @Override
    public void initHeader(Context context, AttributeSet attr) {

    }

    @Override
    public void initFooter(Context context, AttributeSet attr) {

    }

    public void setOnRefreshListener(OnRefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    @Deprecated
    @Override
    public void showHeaderRefresh() {
        //This view is not support pull refresh.
    }
}
